<div class="row animate fadeIn">
        <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>View</i></h5>
            <div class="ibox-tools">
                <button type="button" id="add_prepayment" class="btn btn-sm btn-primary save-btn-tab">Add</button>
                <button type="button" id="save_prepayment" style="display: none;" class="btn btn-sm btn-primary save-btn-tab">Save</button>
                <button type="button" id="cancel_prepayment" style="display: none;" class="btn btn-sm btn-primary save-btn-tab">Cancel</button>
                <a id="cancel_edit" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Close</a>
            </div>
        </div>
        <form class="form-horizontal" id="new_addform"  method="post" style="display:none;">
          <div class="ibox-content">
            <div class="form-group">
              <label class="col-lg-3 control-label">Prepayment No.</label>
                <div class="col-lg-5">
                  <p class="form-control-static" id="prepayment_no"></p>
                </div>
            </div>
           <?php foreach ($datus as $e):?>
                <input type="hidden" name="account_id" value="<?php echo $e->account_id; ?>">
            <?php endforeach; ?>
            <div class="form-group">
              <label class="col-lg-3 control-label">Payment Type</label>
                <div class="col-lg-5">
                  <select class="form-control m-b chosen" name="payment_type">
                      <option value="">--</option>
                      <?php foreach ($paytype as $a):?>
                          <option value ="<?php echo $a->lookup_code; ?>"><?php echo $a->meaning; ?></option>
                      <?php endforeach; ?>
                  </select>
                </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label">Ref No. / Cheque No.</label>
                <div class="col-lg-5">
                  <input type="number" name="ref_no" class="form-control" style="text-align: right;">
                </div>
            </div>
            <div class="form-group">
              <label class="col-lg-3 control-label">Amount</label>
                <div class="col-lg-5">
                  <input type="number" name="amount" class="form-control" style="text-align: right;">
                </div>
            </div>
          </div>
        </form>
          <?php foreach ($datus as $e):?>
            <form class="form-horizontal">
        <div class="ibox-content">
            <div class="form-group"><label class="col-lg-3 control-label">Account</label>
                <div class="col-lg-5">
                          <p class="form-control-static"><b><?php echo "(".$e->acct_num.") ".$e->acct_name; ?></b></p>
                </div>
            </div>
             <div class="form-group"><label class="col-lg-3 control-label">Remaining Amount</label>
                <div class="col-lg-10 input-s">
                          <p class="form-control-static"><b><?php echo "P ".$e->amount; ?></b></p>
                </div>
            </div>
          </div>
          </form>
        <?php endforeach; ?>
           </div>
          <div class="ibox-content">
              <table class="table">
                <thead>
                    <tr>
                        <th>Prepayment No.</th>
                        <th>Payment Type</th>
                        <th>Ref No. / Cheque No.</th>
                        <th>Amount</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                  <?php foreach ($datus2 as $d):?>
                    <tr>
                      <td align="right"><?php echo $d->prepayment_no; ?></td>
                      <td><?php echo $d->paytype; ?></td>
                      <td align="right"><?php echo $d->ref_no; ?></td>
                      <td align="right"><?php echo $d->amount; ?></td>
                      <td><?php echo $d->created_date; ?></td>
                    </tr>
                  <?php endforeach; ?>
                </tbody>
              </table>
          </div>
</div>
      <script>
            $(document).ready(function () {
              $(".chosen").chosen({width:"100%;"});
              $(".chosen1").chosen({width:"100%;","disable_search": true});
              $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green',
              });

              $('#add_prepayment').click(function(){
                  $('#new_addform').slideDown();
                  $('#cancel_prepayment').show();
                  $('#save_prepayment').show();
                  $(this).hide();
              });

              $('#cancel_prepayment').click(function(){
                $('#add_prepayment').show();
                $('#save_prepayment').hide();
                $('#new_addform').slideUp();
                $(this).hide();
              });

              $("#save_prepayment").click(function(){
                 if($("#new_addform").valid() == true ){
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax').'/insert'); ?>",
                        data: $('#new_addform').serialize(),
                        dataType: 'json',
                        success: function(data){
                          if(data.err == 1){
                            $("#save_prepayment").removeAttr('disabled');
                            notifymsg("danger",data.msg);
                          }else{
                            notifymsg("success","Added Successfully!");
                            $('#cancel_prepayment').hide();
                            $('#add_prepayment').show();
                            $('#save_prepayment').hide();
                            $('#new_addform').slideUp();
                            $('#new_addform #prepayment_no').text(data.payment_no);
                            $("#new_addform #done_btn").show();
                            $("#new_addform #cancel_add").hide();
                            $("#save_prepayment").removeAttr('disabled');
                          }
                        },
                        beforeSend: function(){
                           $("#save_prepayment").attr('disabled',true);
                        }
                    });
                 }
               });

               $("#new_addform").validate({
                     rules: {
                         amount: {
                             required: true
                         },
                         payment_type: {
                          required: true
                         }
                     }
                });

              $("#ap_editform input[name=new_amount]").keyup(function(event) {
                if($(this).val() < 0){
                  notifymsg("danger",'Invalid.');
                }else{
                  notifymsg("hide",'hide');
                  calc($(this).val());
                }
              });

              function calc(src) {
                  var sum = 0;
                  var bal = $('input[name="balance"]').val();

                  if(!isNaN(src) && src.length!=0) {
                      sum += parseFloat(src);
                  }
                  $('#ap_editform input[name=totalcost]').val(parseFloat(sum) + parseFloat(bal));
                  bal = bal - sum;
                  if(bal < 0 || sum < 0){
                      bal = 0;
                  }
                  //$('#ap_editform input[name="totalcost"]').val(bal.toFixed(2));
              }

               $("#addLookUpForm").validate({
                rules:{
                    email: {
                        required: true
                    }
                  }
                });

               $(document).on('click','.onoffswitch-checkbox',function(e){
                  if($(this).attr('id') != 'main'){
                    $('#luvae input, #luvae select').prop('disabled', true);
                    console.log('pesti');
                  }
               });

               $("#ap_editform").validate({
                rules:{
                    new_amount: {
                        required: true,
                        number: true
                    }
                  }
                });
        $('#saveLookUp_edit').click(function(){
          if($("#ap_editform").valid() == true){
            if(!($("#ap_editform input[name=new_amount]").val() < 0)){
              $.ajax({
                type: "POST",
                url: "<?php echo base_url($this->session->userdata('forajax') . '/update'); ?>",
                cache: false,
                dataType: 'json',
                data: $('#ap_editform').serialize(),
                success: function(e) {
                  console.log(e);
                  $("#saveLookUp_edit").removeAttr('disabled');
                  if(e.err == 0){
                    notifymsg("success","Success");
                    $('#report_modale').modal('hide');
                  }else{
                    notifymsg("danger",e.msg);
                  }
                },
                beforeSend: function(){
                  $("#saveLookUp_edit").attr('disabled',true);
                }
              });
            }else{
            }
          }
        });
                  $('#cancel_edit').click(function () {
                              swal({
                                          title: "Are you sure?",
                                          text: "",
                                          type: "warning",
                                          showCancelButton: true,
                                          confirmButtonColor: "#DD6B55",
                                          confirmButtonText: "Leave",
                                          cancelButtonText: "Stay",
                                          closeOnConfirm: true,
                                          closeOnCancel: true },
                                      function (isConfirm) {
                                          if (isConfirm) {
                                            swal("success");
                                              close_edit(); active_search();
                                              notifymsg("hide","hide");
                                          } else {
                                              swal("Cancelled");
                                          }
                                      });
                          });

            });
        $('#editX').click(function(){
                    close_edit();
                });
        $('#done_btn').click(function(){
            close_edit();
       });
</script>